# !/usr/bin/env python
# -*- coding: UTF-8 -*-
# date: 12/20/11 5:52 PM
# Filename: configure.py

#
# Copyright 2011 Youku
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import sys
import ConfigParser


def help():
    print '''
usage: python configure.py argument

Avaiable arguments:
  -p [-production]         : run tornado for product envirnment
  -t [-test]               : run tornado for product envirnment
    '''

_argument_production = ['-p', '-production']
_argument_test = ['-t', '-test']
_arguments = {'production': _argument_production, 'test': _argument_test}

def production():
    config = ConfigParser.RawConfigParser()

    ### config for mysql ###
    section = 'Mysql'
    config.add_section(section)
    config.set(section, 'port', '3306')
    config.set(section, 'host', '10.103.13.14')
    config.set(section, 'user', 'stat')
    config.set(section, 'password', '123456')
    config.set(section, 'database', 'mobile_production')

    ### config for tornado ###
    section = 'Tornado'
    config.add_section(section)
    config.set(section, 'port', '8888')
    config.set(section, 'debug', 'false')
    config.set(section, 'processes', '2')

    ### config for log ###
    section = 'Log'
    config.add_section(section)
    config.set(section, 'path', '/opt/logs/stat/logs/')
    config.set(section, 'filename', 'stat.{0}.log')
    config.set(section, 'timeformat', '%Y%m%d')

    ### config for filedb ###
    section = 'DataFile'
    config.add_section(section)
    config.set(section, 'enable', 'false')
    config.set(section, 'path', '/opt/logs/stat/data/')
    config.set(section, 'filename', 'stat.{0}.db')
    config.set(section, 'timeformat', '%Y%m%d')

    ### generat file ###
    with open('../conf/stat.cfg', 'wb') as config_file:
        config.write(config_file)


def test():
    config = ConfigParser.RawConfigParser()

    ### config for mysql ###
    section = 'Mysql'
    config.add_section(section)
    config.set(section, 'port', '3306')
    config.set(section, 'host', '127.0.0.1')
    config.set(section, 'user', 'stat')
    config.set(section, 'password', '123456')
    config.set(section, 'database', 'mobile_production')

    ### config for tornado ###
    section = 'Tornado'
    config.add_section(section)
    config.set(section, 'port', '8888')
    config.set(section, 'debug', 'true')
    config.set(section, 'processes', '1')

    ### config for log ###
    section = 'Log'
    config.add_section(section)
    config.set(section, 'path', './')
    config.set(section, 'filename', 'stat.{0}.log')
    config.set(section, 'timeformat', '%Y%m%d')

    ### config for filedb ###
    section = 'DataFile'
    config.add_section(section)
    config.set(section, 'enable', 'true')
    config.set(section, 'path', './')
    config.set(section, 'filename', 'stat.{0}.db')
    config.set(section, 'timeformat', '%Y%m%d')

    ### generat file ###
    with open('../conf/stat.cfg', 'wb') as config_file:
        config.write(config_file)

if __name__ == '__main__':
    if(len(sys.argv)) != 2:
        help()
        sys.exit()

    if sys.argv[1] in _arguments.get('production'):
        production()
    elif sys.argv[1] in _arguments.get('test'):
        test()
